package models

import (
	"gintest/database"
)
type ParentStoreCustomClass struct{
	Id        int    `json:"id" gorm:"column:id;primary_key" form:"id"`
	Title string `json:"title" gorm:"column:title" form:"title"`
	Sort  int `json:"sort" gorm:"column:sort" form:"sort"`
}
func (ParentStoreCustomClass) TableName() string {
	return "store_custom_class"
}
//表结构
type StoreCustomClass struct {
	Id        int    `json:"id" gorm:"column:id;primary_key" form:"id"`
	StoreId  int `json:"store_id" gorm:"column:store_id" form:"store_id"`
	ParentId  int `json:"parent_id" gorm:"column:parent_id" form:"parent_id"`
	Title string `json:"title" gorm:"column:title" form:"title"`
	Sort  int `json:"sort" gorm:"column:sort" form:"sort"`

	ParentStoreCustomClass ParentStoreCustomClass `json:"parent_storecustom_class" gorm:"ForeignKey:ParentId"`
}
func (StoreCustomClass) TableName() string {
	return "store_custom_class"
}

func GetStoreCustomClassList(page uint64,pagesize uint64,keyword string,store_id uint64)(data []StoreCustomClass,total uint64){
	var offset=(page-1)*pagesize
	db:=database.SqlDB.Model(StoreCustomClass{})

	if len(keyword)!=0{
		db=db.Where("title like ?","%"+keyword+"%")
	}
	if store_id>0{
		db=db.Where("store_id = ?",store_id)
	}

	db.Offset(offset).Limit(pagesize).Order("id desc").Preload("ParentStoreCustomClass").Find(&data)
	db.Model(StoreCustomClass{}).Count(&total)

	return data,total
}
func AddStoreCustomClass(data *StoreCustomClass) error{
	return database.SqlDB.Model(StoreCustomClass{}).Omit("ParentStoreCustomClass").Create(data).Error
}
func DelStoreCustomClassByIds(ids []int) error{
	return database.SqlDB.Model(StoreCustomClass{}).Where("id in (?)",ids).Delete(StoreCustomClass{}).Error
}
func GetStoreCustomClassById(id int) (data StoreCustomClass){
	database.SqlDB.Model(StoreCustomClass{}).Where("id=?",id).Preload("ParentStoreCustomClass").First(&data);
	return data
}
func EditStoreCustomClassById(id int,data interface{}) error{
	return database.SqlDB.Model(&StoreCustomClass{}).Where("id=?",id).Updates(data).Error
}